package org.fullhappy.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.fullhappy.dao.CategoryDAO;
import org.fullhappy.dao.GalleryDAO;
import org.fullhappy.dao.ProductDAO;
import org.fullhappy.entity.Category;
import org.fullhappy.entity.Gallery;
import org.fullhappy.entity.Product;

@Path("/process")
public class ActionProcess {
	@Context
	HttpServletRequest request;

	//gallery{
	
	@POST
	@Path("/add_gallery")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public Response addGallery(
			@FormParam("new_gallery_name") String newGalleryName) {
		GalleryDAO galleryDAO = new GalleryDAO();
		Gallery gallery = new Gallery();
		gallery.setDisable(false);
		gallery.setName(newGalleryName);
		try {
			galleryDAO.insert(gallery);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Gallery> list = galleryDAO.findAll();
		return Response.status(Response.Status.OK)
				.entity("{'nn':'" + list.size() + "'}").build();
	}
	
	@POST
	@Path("/edit_gallery")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public Response editGallery(@FormParam("gallery_id") long galleryId,
			@FormParam("new_gallery_name") String newGalleryName) {
		GalleryDAO galleryDAO = new GalleryDAO();
		Gallery gallery = galleryDAO.find(galleryId);
		gallery.setDisable(false);
		gallery.setName(newGalleryName);
		try {
			galleryDAO.update(gallery);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Gallery> list = galleryDAO.findAll();
		return Response.status(Response.Status.OK)
				.entity("{'nn':'" + list.size() + "'}").build();
	}
	
	@POST
	@Path("/delete_gallery")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public Response deleteGallery(@FormParam("gallery_id") long galleryId) {
		GalleryDAO galleryDAO = new GalleryDAO();
		Gallery gallery = galleryDAO.find(galleryId);
		try {
			galleryDAO.update(gallery);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Gallery> list = galleryDAO.findAll();
		return Response.status(Response.Status.OK)
				.entity("{'nn':'" + list.size() + "'}").build();
	}
	
	//}gallery
	
	//category{
	@POST
	@Path("/add_category")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response addCategory(
			@FormParam("gallery_id") long galleryId, @FormParam("new_category_name") String newCategoryName) {
		CategoryDAO categoryDAO = new CategoryDAO();
		Category category = new Category();
		category.setGalleryId(galleryId);		
		category.setName(newCategoryName);
		category.setDisable(false);
		try {
			categoryDAO.insert(category);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Category> list = categoryDAO.findAll();
		return Response.status(Response.Status.OK)
				.entity("{'size':'" + list.size() + "'}").build();
	}

	@POST
	@Path("/edit_category")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response editCategory(
			@FormParam("category_id") long categoryId, @FormParam("new_category_name") String newCategoryName) {
		CategoryDAO categoryDAO = new CategoryDAO();
		Category category = categoryDAO.find(categoryId);		
		category.setName(newCategoryName);
		category.setDisable(false);
		try {
			categoryDAO.update(category);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Category> list = categoryDAO.findAll();
		return Response.status(Response.Status.OK)
				.entity("{'size':'" + list.size() + "'}").build();
	}
	
	@POST
	@Path("/delete_category")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response deleteCategory(
			@FormParam("category_id") long categoryId) {
		CategoryDAO categoryDAO = new CategoryDAO();
		Category category = categoryDAO.find(categoryId);		
		try {
			categoryDAO.delete(category);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Category> list = categoryDAO.findAll();
		return Response.status(Response.Status.OK)
				.entity("{'size':'" + list.size() + "'}").build();
	}
	
	//}category
	
	//product{
	@POST
	@Path("/add_product")
	//@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	//'category_id='+CategoryId+'&product_name='+productName+'&product_description="'+productDescription+'"&image_urls="'+imageURLs+'"'
	public Response addProduct(@FormParam ("category_id") Long categoryId,@FormParam ("product_name") String productName,
			@FormParam ("product_description") String productDescription, @FormParam ("product_price")Long  productPrice,
			@FormParam ("image_urls") String imageURLs) {	
		
		ProductDAO productDAO = new ProductDAO();
		Product product = new Product();
		product.setCategoryId(categoryId);
		product.setName(productName);
		product.setDisable(false);
		product.setCreatedTime(System.currentTimeMillis());
		product.setDescription(productDescription);
		product.setPrice(productPrice);
		product.setImageURLs(imageURLs);
		
		try {
			productDAO.insert(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Response.status(Response.Status.OK)
				.entity("{'nn':'" + 11 + "'}").build();
	}
	
	@POST
	@Path("/edit_product")
	//@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	//'category_id='+CategoryId+'&product_name='+productName+'&product_description="'+productDescription+'"&image_urls="'+imageURLs+'"'
	public Response editProduct(@FormParam ("product_id") Long productId, @FormParam ("category_id") Long categoryId,@FormParam ("product_name") String productName,
			@FormParam ("product_description") String productDescription, @FormParam ("product_price")Long  productPrice,
			@FormParam ("image_urls") String imageURLs) {	
		
		ProductDAO productDAO = new ProductDAO();
		Product product = productDAO.find(productId);
		if(product!=null){
			if(categoryId!=null){
				product.setCategoryId(categoryId);
			}
			if(productName!=null){
				product.setName(productName);
			}
			if(productDescription!=null){
				product.setDescription(productDescription);
			}
			if(productPrice!=null){
				product.setPrice(productPrice);
			}
			if(imageURLs!=null){
				product.setImageURLs(imageURLs);
			}
		}		

		try {
			productDAO.update(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Response.status(Response.Status.OK)
				.entity("{'product_id':'" + product.getId() + "'}").build();
	}
	
	@POST
	@Path("/delete_product")
	//@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")	
	public Response deleteProduct(@FormParam ("category_id") Long categoryId) {	
		
		ProductDAO productDAO = new ProductDAO();
		Product product = new Product();
		product.setCategoryId(categoryId);		
		
		try {
			productDAO.delete(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Response.status(Response.Status.OK)
				.entity("{'nn':'" + 11 + "'}").build();
	}
	
	//}product
	
	@POST
	@Path("/test1")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public void execute(@FormParam("someparameter") String param) {
		System.out.println(param);
	}
}
